<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" ref="queryForm">
      <el-form-item label="字典名称" prop="name">
        <el-input @keyup.enter.native="handleQuery" clearable placeholder="请输入字典名称" v-model="queryParams.name" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" type="primary">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-20">
      <el-button @click="handleAdd" icon="el-icon-plus" plain type="primary">新增</el-button>
      <el-button :disabled="single" @click="handleDelete" icon="el-icon-delete" plain type="danger">删除</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table :data="pageList" @selection-change="handleSelectionChange" border ref="table" v-loading="loading">
      <el-table-column align="center" type="selection" />
      <el-table-column label="字典名称" prop="name">
        <template slot-scope="scope">
          <a @click="handleRowClick(scope.row)" class="link">{{scope.row.name}}</a>
        </template>
      </el-table-column>
      <el-table-column label="字典编码" prop="code" />
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <el-switch :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row)" v-model="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button @click.stop="handleUpdate(scope.row)" circle icon="el-icon-edit" plain type="primary" />
          <el-button @click.stop="handleDelete(scope.row)" circle icon="el-icon-delete" plain type="danger" />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :limit.sync="pagination.size"
      :page.sync="pagination.current"
      :total="pagination.total"
      @pagination="handleQuery"
      v-show="pagination.total>0"
    />

    <!-- 弹窗表单 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" @close="closeDialog" width="35%">
      <el-form :model="form" :rules="rules" label-width="25%" ref="form">
        <el-form-item label="字典名称" prop="name">
          <el-input placeholder="请输入字典名称" v-model="form.name" />
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input placeholder="请输入字典编码" v-model="form.code" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input placeholder="请输入内容" type="textarea" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="handleSubmit" type="primary">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Api from '../api'
export default {
  name: 'Dict',
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      queryParams: {
        name: undefined,
        queryMode: '0'
      },
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入字典编码', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.current = this.pagination.current
      this.queryParams.size = this.pagination.size
      Api.getDicts(this.queryParams).then(data => {
        this.pageList = data.records
        this.pagination.total = data.total
      }).finally(() => { this.loading = false })
    },
    handleReset() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0
      }
      this.queryParams = {
        name: undefined,
        queryMode: '0'
      }
      this.handleQuery()
    },
    handleRowClick(row) {
      this.$emit('dictClick', row)
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
    },
    handleStatusChange(row) {
      const text = row.status === 0 ? '启用' : '停用'
      this.$confirm('确认要"' + text + row.name + '"数据项吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return Api.addOrUpdateDict({ ...row, status: row.status })
      }).then(() => {
        this.$message.success(text + '成功')
      }).catch(function () {
        row.status = row.status === 1 ? 0 : 1
      })
    },

    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增字典',
        visible: true
      }
    },

    handleUpdate(row) {
      this.resetForm()
      this.dialog = {
        title: '修改字典',
        visible: true
      }
      Api.getDictDetail(row.id).then(data => {
        this.form = data
      })
    },

    handleSubmit: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          Api.addOrUpdateDict(this.form).then(() => {
            this.$message.success(this.form.id ? '修改成功' : '新增成功')
            this.closeDialog()
            this.handleQuery()
          })
        }
      })
    },

    handleDelete(row) {
      const ids = [row.id || this.ids]
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Api.removeDicts(ids).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      }).catch(() =>
        this.$message.info('已取消删除')
      )
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    },
    resetForm() {
      this.form = {
        status: 0
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.link {
  cursor: pointer;
  text-decoration: underline;
  color: $color-theme;
}
</style>
